/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wli.web.java.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author wli
 */
public class FileReaderUtil {

    private String filename;
    private StringBuffer content;
    private Vector<String> v;

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public StringBuffer getContent() {
        File file = new File(filename);
        content = new StringBuffer();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            v = new Vector<String>();
            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                v.add(text);
                content.append(text).append(
                        System.getProperty("line.separator"));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }

    public StringBuffer getContent(String line_separator) {
        File file = new File(filename);
        content = new StringBuffer();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                content.append(text).append(line_separator);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }

    public StringBuffer getContent(String url, String target, boolean include) {
        File file = new File(filename);
        content = new StringBuffer();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                if (include) {
                    content.append("<a href=" + url + text + " target="
                            + target + ">" + text + "<a><br>");
                } else {
                    content.append("<a href=" + url + " target=" + target + ">"
                            + text + "<a><br>");
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }

    public Vector<String> getWordList() {
        return v;
    }
}